package com.cskaoyan.market.config;



import com.cskaoyan.market.shiro.AdminRealm;
import com.cskaoyan.market.shiro.MarketAuthenticator;
import com.cskaoyan.market.shiro.WxRealm;
import org.apache.shiro.authc.Authenticator;
import org.apache.shiro.mgt.SecurityManager;
import org.apache.shiro.realm.Realm;
import org.apache.shiro.session.mgt.SessionManager;
import org.apache.shiro.spring.security.interceptor.AuthorizationAttributeSourceAdvisor;
import org.apache.shiro.spring.web.ShiroFilterFactoryBean;
import org.apache.shiro.web.mgt.DefaultWebSecurityManager;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;



@Configuration
public class ShiroConfig {

    @Bean
    public SecurityManager securityManager(AdminRealm adminRealm, SessionManager sessionManager,Authenticator authenticator){
        DefaultWebSecurityManager securityManager = new DefaultWebSecurityManager();
        securityManager.setSessionManager(sessionManager);
        securityManager.setRealm(adminRealm);
        securityManager.setAuthenticator(authenticator);
        return securityManager;
    }

    @Bean
    public Authenticator authenticator(AdminRealm adminRealm, WxRealm wxRealm){
        MarketAuthenticator marketAuthenticator = new MarketAuthenticator();
        List<Realm> realmList = new ArrayList<>();
        realmList.add(adminRealm);
        realmList.add(wxRealm);
        marketAuthenticator.setRealms(realmList);
        return marketAuthenticator;
    }



    @Bean
    public ShiroFilterFactoryBean shiroFilter(SecurityManager securityManager){
        ShiroFilterFactoryBean factoryBean = new ShiroFilterFactoryBean();
        factoryBean.setSecurityManager(securityManager);
        LinkedHashMap<String, String> filterChainDefinitionMap = new LinkedHashMap<>();
        factoryBean.setFilterChainDefinitionMap(filterChainDefinitionMap);
        return factoryBean;
    }


    @Bean
    public AuthorizationAttributeSourceAdvisor authorizationAttributeSourceAdvisor(SecurityManager securityManager){
        AuthorizationAttributeSourceAdvisor advisor = new AuthorizationAttributeSourceAdvisor();
        advisor.setSecurityManager(securityManager);
        return advisor;
    }
}
